package com.kingyun.gpsinspection.purificationservice.services.tdm.innerservice.impl;

import com.alibaba.fastjson.JSON;
import com.cqhilink.api.common.utils.UniqueUtil;
import com.kingyun.gpsinspection.purificationservice.dao.mapper.tdm.TdmFileMapper;
import com.kingyun.gpsinspection.purificationservice.facade.model.jsm.EmployeeVo;
import com.kingyun.gpsinspection.purificationservice.common.utils.FileTypeUtil;
import com.kingyun.gpsinspection.purificationservice.dao.mapper.tdm.ProductionCommandMapper;
import com.kingyun.gpsinspection.purificationservice.facade.model.File;
import com.kingyun.gpsinspection.purificationservice.facade.model.tdm.ProductionCommand;
import com.kingyun.gpsinspection.purificationservice.facade.model.tdm.ProductionCommandVo;
import com.kingyun.gpsinspection.purificationservice.facade.service.tdm.ProductionCommandService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by liyi on 2016/11/21.
 * 生产指令 业务层
 */
public class ProductionCommandServiceImpl implements ProductionCommandService {
    @Autowired
    public ProductionCommandMapper productionCommandMapper;
    @Autowired
    private TdmFileMapper tdmFileMapper;
    /**
     * 查看
     *
     * @param employeeVo
     * @param productionCommand
     * @return
     * @throws Exception
     */
    @Override
    public Map<String, Object> list(EmployeeVo employeeVo, ProductionCommandVo productionCommand) throws Exception{
        Map<String, Object> map = new HashMap<>();
        productionCommand.setLimit( productionCommand.getLimit() == null ? 10 : productionCommand.getLimit() );
        productionCommand.setOffset( productionCommand.getOffset() == null ? 0 : productionCommand.getOffset() );
        //设置过滤条件
        productionCommand.setFilterEmployeeId(employeeVo.getEmployeeId());
        productionCommand.setFilterOrgId(employeeVo.getOrgId());
        //查询
        System.out.println("插入前主键为："+productionCommand.getCommandId());
        map.put("total",productionCommandMapper.queryCount(productionCommand));
        map.put("rows", JSON.toJSON(productionCommandMapper.queryList(productionCommand)));
        return map;
    }

    /**
     * 根据ID查询
     * @param commandId
     * @return
     * @throws Exception
     */
    @Override
    public ProductionCommandVo findProductionCommandById(String commandId) throws Exception {
        // 根据生产指令ID查询生产计划信息并返回信息
        return productionCommandMapper.selectByPrimaryKeyNew(commandId);
    }

    /***
     * 查询所有生产指令
     * @return
     * @throws Exception
     */
    @Override
    public List<ProductionCommand> queryAll() throws Exception {
        // 查询所有生产指令信息并返回信息
        return productionCommandMapper.queryAll();
    }

    /**
     * 保存
     * 注意：解开启事务，不能抛异常
     *
     *
     * @param employeeVo
     * @param productionCommand
     * @param uploadFiles
     * @param deletedFiles
     * @return
     * @throws Exception
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public int save(EmployeeVo employeeVo, ProductionCommand productionCommand, String uploadFiles, String deletedFiles) {
        // 设置ID
        productionCommand.setCommandId(UniqueUtil.uuid());
        // 设置创建人ID
        productionCommand.setCreateUserId(employeeVo.getEmployeeId());
        // 设置创建时间
        productionCommand.setCreateTime(new Date());
        // 设置修改人ID
        productionCommand.setModifyUserId(employeeVo.getEmployeeId());
        // 设置修改时间
        productionCommand.setModifyTime(new Date());
        productionCommand.setIsDelete((short)0);
        // 保存生产指令信息
        int a = productionCommandMapper.insertSelective(productionCommand);
        if (StringUtils.isNotBlank(uploadFiles)) {
            // 强制转换成集合
            List<File> listFiles = JSON.parseArray(uploadFiles, File.class);
            // 重新设置附件属性
            for (int i = 0; i < listFiles.size(); i++) {
                File _file = listFiles.get(i);
                // 设置主键
                _file.setFileId(UniqueUtil.uuid());
                // 设置业务数据id
                _file.setBusinessId(productionCommand.getCommandId());
                _file.setFileType((short) FileTypeUtil.getFileTypeByFileName(_file.getFileName()));
            }
            // 必须判断非空
            if (listFiles != null && listFiles.size() > 0) {
                a += tdmFileMapper.insertByList(listFiles);
            }
        }
        return a;
    }

    /**
     * 更新
     * @param employeeVo
     * @param productionCommand
     * @param deletedFiles
     * @throws Exception
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public int update(EmployeeVo employeeVo, ProductionCommand productionCommand, String uploadFiles, String deletedFiles) {
        // 判空
        if(productionCommand==null){
            return 0;
        }
        productionCommand.setModifyUserId(employeeVo.getEmployeeId());
        productionCommand.setModifyTime(new Date());
        int a=productionCommandMapper.updateByPrimaryKeySelective(productionCommand);
        // 更新附件信息
        if(StringUtils.isNotBlank(uploadFiles)){
            // 强制转换成集合
            List<File> listFiles =  JSON.parseArray(uploadFiles,File.class);
            if(listFiles!=null && listFiles.size()>0){
                // 重新设置附件属性
                String pId = productionCommand.getCommandId();
                int len = listFiles.size();
                for(int i=0;i<len;i++){
                    File _file = listFiles.get(i);
                    // 设置主键
                    _file.setFileId(UniqueUtil.uuid());
                    // 设置业务数据id
                    _file.setBusinessId(pId);
                    _file.setFileType((short) FileTypeUtil.getFileTypeByFileName(_file.getFileName()));
                }
                a += tdmFileMapper.insertByList(listFiles);
            }
        }
        // 需要删除的附件信息
        if(deletedFiles!=null && !deletedFiles.equals("")){
            // 强制转换成集合
            List<File> listFiles =  JSON.parseArray(deletedFiles,File.class);
            if(listFiles!=null && listFiles.size()>0){
                int len = listFiles.size();
                for(int i=0;i<len;i++){
                    File _file = listFiles.get(i);
                    tdmFileMapper.deleteByPrimaryKey(_file.getFileId());
                }
            }
        }
        return a;
    }

    /**
     * 删除:软删除
     * @param list
     * @return
     */
    @Override
    public int delByIds(List<String> list) throws Exception {
        // 判断生产指令集合是否为空
        if(list==null || (list!=null && list.size()==0)){
            return 0;
        }
        // 修改删除状态
        int i=productionCommandMapper.updateStateByIds(list);
        return i;
    }
}